<?php 
	include("library/vars.php");
	
	if(isset($_POST['get'])) { // If selected to get episode details...
	
		$episode_nr = $_POST['episode_nr'];
		
		if ($episode_nr > 0) {
			$getshow = $show;
			include("library/parse_fix.php");
			include("library/get_episode.php");
		}
						
	}
	
	if(isset($_POST['submit'])) { // If the form is submited
	
		$episode_nr = $_POST['episode_nr'];
		$title = addslashes($_POST['title']);
		$description = addslashes($_POST['description']);
	
		include("library/config.php");
		include("library/opendb.php");
		
		// Check to see if the episode already exists
		$query="SELECT * FROM episodes WHERE episode='$episode_nr' AND season_ID='$season' AND show_ID='$show'";
		$result = mysql_query($query);
		$num=mysql_numrows($result);
	
		if ($num==0) { // If there's no episode with that number...
			$query2 = "INSERT INTO episodes (show_ID, season_ID, episode, title, description) VALUES ('$show', '$season', '$episode_nr', '$title', '$description')";
			mysql_query($query2) or die('Database Error');

			include 'library/closedb.php';
			header( 'Location: index.php?s=' . $show . '&t=' . $season . '&m=episode_added' );
		} 
		else {
			include 'library/closedb.php';
			header( 'Location: add_episode.php?s=' . $show . '&t=' . $season . '&e=' . $episode_nr . '&m=episode_already_exists' );
		}
	}
	
	else { // Show Form
	
		// Get the Show data
		include("library/config.php");
		include("library/opendb.php");
	
		$query="SELECT id, show_name, show_nicename path FROM shows WHERE show_nicename='$show'";
		$result = mysql_query($query);
	
		$query2="SELECT season, show_ID FROM seasons WHERE season='$season' AND show_ID='$show'";
		$result2 = mysql_query($query2);
	
		$i=0;
		$showname=mysql_result($result,$i,"show_name");
		$season_name=mysql_result($result2,$i,"season");
		
		include("library/closedb.php");
		include("library/header.php");
?>
	
<div id="main">
	<div id="seasons">
		<h2><?php echo _("Add New Episode to"); ?>: <strong><?=$showname?> <small>(<?php echo _("Season"); ?> <?=$season_name?>)</small></strong></h2>
	
	</div>
	
	<div id="messages">
		<?php include ("library/messages.php");	?>
	</div>
	
	<form method="post" autocomplete="off" id="add" action="">
		<p><label for="episode_nr"><?php echo _("Episode Number"); ?>:</label> <input style="width:25px" name="episode_nr" type="text" id="episode_nr" tabindex="1" class="{required:true,digits:true}" value="<?=$episode_nr;?>" /></p>
		<p class="get_episode"><input type="submit" name="get" class="get" id="get" value="<?php echo _("Import episode data"); ?>" /> <?php echo _("or add it manually bellow"); ?>:</p>
		<p><label for="title"><?php echo _("Title"); ?>:</label> <input name="title" style="width:400px" type="text" id="title" tabindex="2" value="<?=$title;?>" /></p>
		<p><label for="description"><?php echo _("Description"); ?>:</label> <textarea name="description" id="description" rows="8" cols="40" tabindex="3"><?=$summary;?></textarea></p>
	
		<p class="submit"><input type="submit" name="submit" class="submit" id="submit" value="<?php echo _("Add New Episode"); ?>" tabindex="4" /> <?php echo _("or"); ?> <a href="index.php?s=<?=$show?>&amp;t=<?=$season?>"><?php echo _("Cancel"); ?></a></p>
	</form>
	
	<div id="import_notice">
		<p><?php echo _("<strong>Importing data...</strong><br />Please wait."); ?></p>
	</div>
	
	<div class="push"></div>

</div><!-- /#main -->
<?php include("library/footer.php");?>
</body>
</html>
<?php } ?>